package org.forwhat.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.forwhat.bean.R;
import org.forwhat.bean.WhatPost;
import org.forwhat.bean.WhatTag;
import org.forwhat.bean.WhatUser;
import org.forwhat.framework.annotation.InsertBean;
import org.forwhat.framework.annotation.RequestBody;
import org.forwhat.framework.annotation.WebServletAnnotation;
import org.forwhat.service.WhatPostService;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @projectName: ForWhat
 * @package: org.forwhat.controller
 * @className: PostServlet
 * @author: LGJ
 * @description: TODO
 * @date: 2023/11/23 22:30
 * @version: 1.0
 */
@WebServletAnnotation(value = "PostServlet", url = "/post/*")
public class PostServlet extends BaseServlet {
    @InsertBean("WhatPostServiceImpl")
    private WhatPostService whatPostService;

    protected R getpost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String parameterStart = request.getParameter("start");
        String parameterCount = request.getParameter("count");
        String parameterSearch = request.getParameter("searchContext");

        int start = 0;
        int count = 10;
        if (parameterStart != null) {
            start = Integer.parseInt(parameterStart);
        }
        if (parameterCount != null) {
            count = Integer.parseInt(parameterCount);
        }
        if (parameterSearch != null && !parameterSearch.equals("")) {
            List<WhatPost> whatPosts = whatPostService.select(start, count,parameterSearch);
            return R.ok(whatPosts);
        }


        List<WhatPost> whatPosts = whatPostService.select(start, count,null);
        return R.ok(whatPosts);
    }

    protected R getpostbyid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String postID = request.getParameter("postId");


        if (postID == null || postID.equals("")) {
            return R.fail("参数错误");
        }

        WhatPost postByPostId = whatPostService.getPostByPostId(postID);

//        System.out.println("postByPostId = " + postByPostId);

        if (postByPostId != null) {
            return R.ok(postByPostId);
        } else {
            return R.fail("帖子不存在");
        }
    }

    protected R gethotpost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String count = request.getParameter("count");
        if (count == null) {
            count = "10";
        }

        int i = 0;
        try {
            i = Integer.parseInt(count);
        } catch (NumberFormatException e) {
            return R.fail("参数错误");
        }

        List<WhatPost> whatPosts = whatPostService.selectHot(i);
        return R.ok(whatPosts);
    }

    @RequestBody(WhatPost.class)
    protected R sendpost(HttpServletRequest request, HttpServletResponse response, WhatPost whatPost) throws IOException {
        HttpSession session = request.getSession(false);
        if (session == null || null == session.getAttribute("user")) {
            //???????????

            return R.noLogin();
        }


        try {

            WhatUser user = (WhatUser) session.getAttribute("user");
            whatPost.setPostUserId(user.getUserId());

            int i = whatPostService.sendPost(whatPost);
            if (i == 1) {
                return R.ok("发帖成功");
            } else {
                return R.fail("发帖失败");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(whatPost);
            return R.fail("未知错误");
        }

    }

    public WhatPostService getWhatPostService() {
        return whatPostService;
    }

    public void setWhatPostService(WhatPostService whatPostService) {
        this.whatPostService = whatPostService;
    }


}
